package org.vt.ece3574.pictoslider;

import org.vt.ece3574.pictoslider.HighScoreManager.Difficulty;

import android.content.Context;
import android.graphics.Bitmap;

/**
 * Runnable class used when a puzzle is solved and a new high score may or may
 * not need to be added. Creates a thumbnail of the puzzle image, and runs in a
 * new thread so the storing of the new high scores does not affect the app's
 * UI.
 * 
 * @author Michael
 */
public class HighScoreUpdater implements Runnable {

	// Thumbnail dimensions
	private static final int THUMBNAIL_HEIGHT = 100;
	private static final int THUMBNAIL_WIDTH = 100;

	/**
	 * The application context. Required by the HighScoreManager.
	 */
	private Context context;
	/**
	 * The thumbnail for the recently solved puzzle.
	 */
	private Bitmap image;
	/**
	 * The score for the recently solved puzzle.
	 */
	private long score;
	/**
	 * The number of moves the user took to solved the puzzle.
	 */
	private int moves;
	/**
	 * This Updater will update high scores with this Difficulty.
	 */
	private Difficulty difficulty;

	/**
	 * Constructor for the class. Creates a thumbnail of the bitmap in i.
	 * 
	 * @param c
	 *            - The application context.
	 * @param m
	 *            - The number of moves for the recently solved puzzle.
	 * @param s
	 *            - The score for the recently solved puzzle.
	 * @param i
	 *            - The image for the recently solved puzzle.
	 */
	public HighScoreUpdater(Context c, int m, long s, Bitmap i, Difficulty d) {
		context = c;
		score = s;
		image = Bitmap.createScaledBitmap(i, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
				false);
		moves = m;
		difficulty = d;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		// Update the high scores in a seperate thread.
		HighScoreManager hsm = new HighScoreManager(context);
		hsm.update(moves, score, image, difficulty);
	}

}
